<script setup lang="ts">
import { usePrintStore } from '@/stores'
import type { FormInstance, FormRules } from 'element-plus'
import { DY_MAP_VisibleScales } from '@/constants'

const printtingStore = usePrintStore()
const emit = defineEmits(['update:modelValue'])
const additionInfoForm = reactive<AdditionInfo>({
  additionMapScale: '1',
  centerPoint: '1'
})
const additionInfoFormRef = ref<FormInstance>()
const rules = reactive<FormRules<AdditionInfo>>({
  additionMapScale: [{ required: true, message: '请填写附图比例尺', trigger: 'blur' }],
  centerPoint: [{ required: true, message: '请填写中心点', trigger: 'blur' }]
})

function toOtherForm(type: any) {
  emit('update:modelValue', type)
}

async function submitAdditionInfoForm(formEl: FormInstance | undefined, type: string) {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (!valid) return
    printtingStore.submitAdditionInfoForm(additionInfoForm)
    emit('update:modelValue', type)
  })
}

onBeforeMount(() => {
  const { lng, lat } = map.getCenter()
  const zoom = map.getZoom()
  additionInfoForm.additionMapScale = DY_MAP_VisibleScales[Math.round(zoom) - 2 < 0 ? 0 : Math.round(zoom) - 2].toString()
  additionInfoForm.centerPoint = `${lng},${lat}`
})

watch(
  () => printtingStore.additionInfo,
  (newVal) => {
    Object.assign(additionInfoForm, newVal)
  }
)
</script>

<template>
  <div>
    <el-form ref="additionInfoFormRef" :rules="rules" :model="additionInfoForm" label-position="right">
      <el-form-item label="小地图比例尺：" prop="additionMapScale" label-width="130px">
        <el-select v-model="additionInfoForm.additionMapScale" placeholder="比例尺选择" style="width: 300px">
          <el-option :label="item" :value="item" :key="item" v-for="(item, index) in DY_MAP_VisibleScales">
            <span style="float: left">{{ item }}</span>
            <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">第 {{ index }} 级比例尺</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="小地图中心点：" prop="centerPoint" label-width="130px">
        <el-input v-model="additionInfoForm.centerPoint" autocomplete="off" style="width: 300px" />
      </el-form-item>
    </el-form>
    <div class="btn-group">
      <el-button type="warning" @click="toOtherForm('basicInfo')" style="margin-right: 50px">上一步</el-button>
      <el-button type="primary" @click="submitAdditionInfoForm(additionInfoFormRef, 'scaleInfo')">下一步</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.btn-group {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
